package com.shiyi.controller.system;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shiyi.common.Constants;
import com.shiyi.common.ResponseResult;
import com.shiyi.service.RedisService;
import com.shiyi.service.WebConfigService;
import com.shiyi.util.WeChatUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.shiyi.common.Constants.*;

/**
 * @author: muxiongxiong
 * @date: 2022年12月20日 22:45
 * 公众号：雄雄的小课堂
 * 博客：https://blog.csdn.net/qq_34137397
 * 个人站：http://www.穆雄雄.com
 * 个人站：http://www.muxiongxiong.cn
 * @Description: 微信控制器
 */
@RestController
@Slf4j
@RequestMapping("/system/wechat")
@Api(tags = "公众号开发")
@RequiredArgsConstructor
public class WechatController {
    private final RedisService redisService;

    /**
     * 获取access_token的值
     * @return
     */
    @GetMapping("/getToken")
    public String getToken() {
        String token = "";
        String path = "token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET;
        String body = HttpUtil.createGet(WX_GZH_API + path)
                .execute()
                .body();
        log.info("获取了token,返回数据" + body);
        JSONObject object = JSON.parseObject(body);
        //获取token
        token = object.getString("access_token");
        //失效时间
        String expires_in = object.getString("expires_in");
        //将token值的值放在redis里面
        redisService.setCacheObject("gzh_access_token", token,7190,TimeUnit.SECONDS);
        return token;

    }

    /**
     * 获取jsapi_ticket
     * @return
     */
        @GetMapping("/getJsapiTicket")
        public String getJsapiTicket() {
            //获取redis里面的token
            Object access_token = redisService.getCacheObject("gzh_access_token");
            if (access_token==null) {
                access_token = getToken();
            }
            String path = "ticket/getticket?access_token=" + access_token.toString() + "&type=jsapi";
            String body = HttpUtil.createGet(WX_GZH_API + path)
                    .execute()
                    .body();
        log.info("获取了JsapiTicket,返回数据" + body);
        JSONObject object = JSON.parseObject(body);
        //获取ticket
        String ticket = object.getString("ticket");
        //错误码
        Integer errcode = object.getInteger("errcode");
        if(errcode==0){
            //将ticket值的值放在redis里面
            redisService.setCacheObject("gzh_ticket", ticket,7190,TimeUnit.SECONDS);
        }
        return ticket;
    }


    /**
     * 开始签名
     */
    @GetMapping("/getSing")
    public ResponseResult getSing(String url){
        //从redis里面获取ticket
        Object ticket = redisService.getCacheObject("gzh_ticket");
        if(ticket==null){
            ticket = getJsapiTicket();
        }
        Map<String, String> ret = WeChatUtils.sign(ticket.toString(), url);
        JSONObject objectData = new JSONObject();
        for (Map.Entry entry : ret.entrySet()) {
            objectData.put(entry.getKey().toString(),entry.getValue());
        }
        objectData.put("appId", APPID);
        return ResponseResult.success(objectData);
    }



}
